package com.sanqing.dao;

import com.sanqing.po.Position;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;

/**
 	* A data access object (DAO) providing persistence and search support for Position entities.
 			* Transaction control of the save(), update() and delete() operations 
		can directly support Spring container-managed transactions or they can be augmented	to handle user-managed Spring transactions. 
		Each of these methods provides additional information for how to configure it for the desired type of transaction control. 	
	 * @see com.sanqing.po.Position
  * @author MyEclipse Persistence Tools 
 */

public class PositionDao extends BaseHibernateDao  {
		 private static final Log log = LogFactory.getLog(PositionDao.class);
		//property constants
	public static final String NAME = "name";
	public static final String PURPOSE = "purpose";
	public static final String CONTENT = "content";
	public static final String DUTY = "duty";
	public static final String SUPERIOR_ID = "superiorId";
	public static final String PLANNUM = "plannum";
	public static final String OCCUNUM = "occunum";



    
    public void save(Position transientInstance) {
        log.debug("saving Position instance");
        try {
        	Session session = getSession();
        	Transaction tx = session.beginTransaction();
            session.save(transientInstance);
            tx.commit();
            closeSession();
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }
    
	public void delete(Position persistentInstance) {
        log.debug("deleting Position instance");
        try {
        	Session session = getSession();
        	Transaction tx = session.beginTransaction();
            session.delete(persistentInstance);
            tx.commit();
            closeSession();
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }
    
	public void update(Position persistentInstance) {
        log.debug("deleting Position instance");
        try {
        	Session session = getSession();
        	Transaction tx = session.beginTransaction();
            session.update(persistentInstance);
            tx.commit();
            closeSession();
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }
	
    public Position findById( java.lang.Integer id) {
        log.debug("getting Position instance with id: " + id);
        try {
            Position instance = (Position) getSession()
                    .get("com.sanqing.po.Position", id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }
    
    
    public List findByExample(Position instance) {
        log.debug("finding Position instance by example");
        try {
            List results = getSession()
                    .createCriteria("com.sanqing.po.Position")
                    .add(Example.create(instance))
            .list();
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }    
    
    public List findByProperty(String propertyName, Object value) {
      log.debug("finding Position instance with property: " + propertyName
            + ", value: " + value);
      try {
         String queryString = "from Position as model where model." 
         						+ propertyName + "= ?";
         Query queryObject = getSession().createQuery(queryString);
		 queryObject.setParameter(0, value);
		 return queryObject.list();
      } catch (RuntimeException re) {
         log.error("find by property name failed", re);
         throw re;
      }
	}

	public List findByName(Object name
	) {
		return findByProperty(NAME, name
		);
	}
	
	public List findByPurpose(Object purpose
	) {
		return findByProperty(PURPOSE, purpose
		);
	}
	
	public List findByContent(Object content
	) {
		return findByProperty(CONTENT, content
		);
	}
	
	public List findByDuty(Object duty
	) {
		return findByProperty(DUTY, duty
		);
	}
	
	public List findBySuperiorId(Object superiorId
	) {
		return findByProperty(SUPERIOR_ID, superiorId
		);
	}
	
	public List findByPlannum(Object plannum
	) {
		return findByProperty(PLANNUM, plannum
		);
	}
	
	public List findByOccunum(Object occunum
	) {
		return findByProperty(OCCUNUM, occunum
		);
	}
	

	public List findAll() {
		log.debug("finding all Position instances");
		try {
			String queryString = "from Position";
	         Query queryObject = getSession().createQuery(queryString);
			 return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
    public Position merge(Position detachedInstance) {
        log.debug("merging Position instance");
        try {
            Position result = (Position) getSession()
                    .merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(Position instance) {
        log.debug("attaching dirty Position instance");
        try {
            getSession().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    
    public void attachClean(Position instance) {
        log.debug("attaching clean Position instance");
        try {
            getSession().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
}